import { Controller } from "../../mvc/core/Controller";
import { RaceCopyIconBtn } from "../view/raceCopy/RaceCopyIconBtn";
import { RaceCopyRoomChooseWin } from "../view/raceCopy/RaceCopyRoomChooseWin";
import { RaceCopyInfoPanel } from "../view/raceCopy/RaceCopyInfoPanel";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { HintIcon } from "../view/uiIconBtn/HintIcon";
import { ImagesConst } from "../resource/ImagesConst";
import { UiHintIconView } from "../view/uiIconBtn/UiHintIconView";
import { Language } from "../../../extend/language/Language";
import { Alert } from "../../../com/mui/controls/Alert";
import { GameProxy } from "../mvc/GameProxy";
import { SRaceRoomInfo } from "../../../Message/Public/SRaceRoomInfo";
import { SRaceRoomPlayerNum } from "../../../Message/Public/SRaceRoomPlayerNum";
import { SRaceResult } from "../../../Message/Public/SRaceResult";
import { TCopy } from "../../../Message/Db/Tables/TCopy";
import { ECopyType } from "../../../Message/Public/ECopyType";
import { DataEvent } from "../events/DataEvent";
import { KeyBoardManager } from "../../../com/gengine/keyBoard/KeyBoardManager";
import { KeyEvent } from "../../../com/gengine/keyBoard/KeyEvent";
import { Game } from "../Game";
import { GameMapUtil } from "../scene/map/GameMapUtil";
import { Global } from "../../../com/gengine/global/Global";
import { Log } from "../../../com/gengine/debug/Log";
import { RaceCopySkillPanel } from "../view/raceCopy/RaceCopySkillPanel";
import { SRaceSkillDrop } from "../../../Message/Public/SRaceSkillDrop";
import { SPlayerItem } from "../../../Message/Public/SPlayerItem";
import { SRaceLastBest } from "../../../Message/Public/SRaceLastBest";
import { SRaceReachPoint } from "../../../Message/Public/SRaceReachPoint";
import { SRaceRooms } from "../../../Message/Public/SRaceRooms";
import { SRaceRoom } from "../../../Message/Public/SRaceRoom";
import { SRaceUseSkill } from "../../../Message/Public/SRaceUseSkill";
import { AIManager } from "../scene/ai/AIManager";
import { AIType } from "../scene/ai/AIType";
import { KeyCode } from "../../../com/gengine/keyBoard/KeyCode";
import { ClockManager } from "../manager/ClockManager";
type int = number;
//class RaceCopyController
    
    export  class RaceCopyController extends Controller
    {
       

        /* internal  */get icon(): RaceCopyIconBtn
        {
            if (this._icon == null) 
            {
                this._icon = new RaceCopyIconBtn();
            }
            return this._icon;
        }

        /* internal  */get winChoose(): RaceCopyRoomChooseWin
        {
            if (this._winChoose == null) 
            {
                this._winChoose = new RaceCopyRoomChooseWin();
            }
            return this._winChoose;
        }

        /* internal  */get infoPanel(): RaceCopyInfoPanel
        {
            if (this._infoPanel == null) 
            {
                this._infoPanel = new RaceCopyInfoPanel();
            }
            return this._infoPanel;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.RaceCopyRaceCountDown, this.raceCountDownHandler);
            NetDispatcher.addCmdListener(ServerCommand.RaceCopyRaceStart, this.raceStartHandler);
            NetDispatcher.addCmdListener(ServerCommand.RaceCopyLastRanking, this.lastRankingHandler);
            NetDispatcher.addCmdListener(ServerCommand.RaceCopyRaceEnd, this.raceEndHandler);
            NetDispatcher.addCmdListener(ServerCommand.RaceCopyRaceReachPoint, this.raceReachPointHandler);
            NetDispatcher.addCmdListener(ServerCommand.RaceCopyRoomsGot, this.roomsGotHandler);
            NetDispatcher.addCmdListener(ServerCommand.RaceCopySkillDrop, this.skillDropHandler);
            NetDispatcher.addCmdListener(ServerCommand.Copy_LeaveCopy, this.onLeaveCopyHandler);
            NetDispatcher.addCmdListener(ServerCommand.Copy_EnterCopy, this.enterCopyHandler);
            NetDispatcher.addCmdListener(ServerCommand.RaceCopyEnterSuccess, this.enterRaceRoomSuccessHandler);
            NetDispatcher.addCmdListener(ServerCommand.RaceCopyRoomUpdate, this.roomInfoUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.RaceCopyRoomPlayerNumUpdate, this.roomPlayerNumUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.RaceCopyRaceResult, this.raceResultHandler);
            NetDispatcher.addCmdListener(ServerCommand.Copy_CopyNumInfoChange, this.onCopyNumChangeHandler);
            Dispatcher.addEventListener(EventName.RaceCopyOpenChoosePanel, this.openRoomChooseHandler);
            Dispatcher.addEventListener(EventName.RaceCopyEnterCopy, this.enterRaceCopyHandler);
            Dispatcher.addEventListener(EventName.RaceCopyUseSkill, this.useSkillHandler);
            Dispatcher.addEventListener(EventName.SkillTransferOver, this.onSkillTransferOverRes);
            return;
        }

        /* internal  */showHintIcon(): void
        {
            if (this._hintIcon == null) 
            {
                this._hintIcon = new HintIcon(ImagesConst.RaceCopyHintIcon);
                this._hintIcon.addEventListener(flash.events.MouseEvent.CLICK, this.onHintIconClickHandler);
            }
            if (this._hintIcon.parent == null) 
            {
                UiHintIconView.instance.addHintIcon(this._hintIcon, true);
            }
            return;
        }

        /* internal  */hideHitIcon(): void
        {
            if (this._hintIcon != null) 
            {
                UiHintIconView.instance.removeHintIcon(this._hintIcon);
            }
            return;
        }

        /* internal  */onHintIconClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._curResultData == null) 
            {
                return;
            }
            this.hideHitIcon();
            var loc1="";
            if (this._curResultData.rank <= 0) 
            {
                loc1 = Language.getString(80166);
            }
            else 
            {
                loc1 = Language.getStringByParam(80165, this._curResultData.rank.toString());
            }
            Alert.show(loc1, null, Alert.OK | Alert.CANCEL, null, this.onCloseResult);
            this._curResultData = null;
            return;
        }

        /* internal  */onCloseResult(arg1: int): void
        {
            if (arg1 != Alert.OK) 
            {
                return;
            }
            GameProxy.copy.leftGroup_async();
            return;
        }

        /* internal  */raceCountDownHandler(arg1: Object): void
        {
            this.icon.show();
            this.updateChooseWinTimer();
            return;
        }

        /* internal  */raceStartHandler(arg1: Object): void
        {
            this.icon.show();
            this.updateChooseWinTimer();
            return;
        }

        /* internal  */enterRaceRoomSuccessHandler(arg1: Object): void
        {
            return;
        }

        /* internal  */roomInfoUpdateHandler(arg1: Object): void
        {
            var loc1=arg1 as SRaceRoomInfo;
            if (this._winChoose) 
            {
                this.winChoose.updateRoomInfo(loc1);
            }
            this.infoPanel.updateRoomInfo(loc1);
            return;
        }

        /* internal  */roomPlayerNumUpdateHandler(arg1: Object): void
        {
            var loc1=arg1 as SRaceRoomPlayerNum;
            if (this._winChoose) 
            {
                this.winChoose.updateRoomPlayerNum(loc1);
            }
            this.infoPanel.updateRoomPlayerNum(loc1);
            return;
        }

        /* internal  */raceResultHandler(arg1: Object): void
        {
            if (!cache.copy.isInRaceCopy()) 
            {
                return;
            }
            var loc1=arg1 as SRaceResult;
            this._curResultData = loc1;
            this.showHintIcon();
            return;
        }

        /* internal  */onCopyNumChangeHandler(arg1: Object): void
        {
            if (!cache.copy.isInRaceCopy()) 
            {
                return;
            }
            if (this._infoPanel != null) 
            {
                this._infoPanel.updateTodayTimes();
            }
            return;
        }

        /* internal  */onLeaveCopyHandler(arg1: Object): void
        {
            var loc1=arg1 as TCopy;
            if (loc1.copyType != ECopyType._ECopyRace) 
            {
                return;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.ShowGameMainUI, true));
            Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTrackShowOrHide, true));
            this.infoPanel.hide();
            this.skillPanel.hide();
            cache.copy.raceCopyDelSkill(null, true);
            this.skillPanel.clearUseLingDong();
            KeyBoardManager.instance.removeEventListener(KeyEvent.KEY_DOWN, this.keyDownHandler);
            this.hideHitIcon();
            return;
        }

        /* internal  */enterCopyHandler(arg1: Object): void
        {
            if (!cache.copy.isInRaceCopy()) 
            {
                return;
            }
            if (Game.isSceneInit() && GameMapUtil.isMapByCopyCode(152)) 
            {
                this.onSceneUpdate(null);
            }
            else 
            {
                Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdate);
            }
            return;
        }

        /* internal  */onSceneUpdate(arg1: DataEvent): void
        {
            Dispatcher.removeEventListener(EventName.Scene_Update, this.onSceneUpdate);
            if (!cache.copy.isInRaceCopy()) 
            {
                return;
            }
            if (this._winChoose) 
            {
                this.winChoose.hide();
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.ShowGameMainUI, false));
            Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTrackShowOrHide, false));
            this.infoPanel.show();
            this.infoPanel.updatePosition();
            this.skillPanel.show(Global.stage.stageWidth - 485, Global.stage.stageHeight - 175);
            var loc1=cache.pack.mountsPackCache.callEdMount;
            if (loc1 == null) 
            {
                Log.debug("进入天神竞技场， 玩家没有骑上坐骑， 系统帮玩家骑上坐骑");
                Dispatcher.dispatchEvent(new DataEvent(EventName.KeyControllMounts));
            }
            KeyBoardManager.instance.addEventListener(KeyEvent.KEY_DOWN, this.keyDownHandler);
            this.infoPanel.updateTodayTimes();
            return;
        }

        /* internal  */get skillPanel(): RaceCopySkillPanel
        {
            if (!this._skillPanel) 
            {
                this._skillPanel = new RaceCopySkillPanel(4);
            }
            return this._skillPanel;
        }

        /* internal  */onEnterTimer(): void
        {
            if (!cache.copy.isInRaceCopy()) 
            {
                return;
            }
            if (this._icon == null && this.count < 20) 
            {
                var loc2;
                var loc3=((loc2 = this).count + 1);
                loc2.count = loc3;
                flash.utils.setTimeout(this.onEnterTimer, 1000);
                return;
            }
            if (this.count >= 20) 
            {
                return;
            }
            if (this._winChoose) 
            {
                this.winChoose.hide();
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.ShowGameMainUI, false));
            Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTrackShowOrHide, false));
            this.infoPanel.show();
            this.infoPanel.updatePosition();
            this.skillPanel.show(Global.stage.stageWidth - 485, Global.stage.stageHeight - 175);
            var loc1=cache.pack.mountsPackCache.callEdMount;
            if (loc1 == null) 
            {
                Log.debug("进入天神竞技场， 玩家没有骑上坐骑， 系统帮玩家骑上坐骑");
                Dispatcher.dispatchEvent(new DataEvent(EventName.KeyControllMounts));
            }
            KeyBoardManager.instance.addEventListener(KeyEvent.KEY_DOWN, this.keyDownHandler);
            return;
        }

        /* internal  */skillDropHandler(arg1: Object): void
        {
            var loc4=null;
            var loc5=null;
            var loc1=[];
            var loc2=cache.copy.raceCopyMySkills;
            var loc3=0;
            while (loc3 < loc2.length) 
            {
                loc4 = loc2[loc3] as SRaceSkillDrop;
                (loc5 = new SPlayerItem()).itemAmount = 1;
                loc5.itemCode = loc4.itemCode;
                loc5.uid = loc4.skillUid;
                loc5.playerId = cache.role.playerInfo.playerId;
                loc5.jsStr = "";
                loc5.posType = 1;
                loc1.push(loc5);
                ++loc3;
            }
            this.skillPanel.setPropListData(loc1);
            return;
        }

        /* internal  */lastRankingHandler(arg1: Object): void
        {
            var loc1=arg1 as SRaceLastBest;
            return;
        }

        /* internal  */raceEndHandler(arg1: Object): void
        {
            this.icon.hide();
            return;
        }

        /* internal  */raceReachPointHandler(arg1: Object): void
        {
            var loc1=arg1 as SRaceReachPoint;
            this.infoPanel.updateFininshProgress(loc1);
            if (loc1.index >= loc1.maxIndex) 
            {
                this.skillPanel.canUseSkill = false;
            }
            else 
            {
                this.skillPanel.canUseSkill = true;
            }
            return;
        }

        /* internal  */roomsGotHandler(arg1: Object): void
        {
            var loc1=arg1 as SRaceRooms;
            if (this._winChoose) 
            {
                this.winChoose.updateWiew(loc1);
            }
            this.updateChooseWinTimer();
            return;
        }

        /* internal  */openRoomChooseHandler(arg1: DataEvent): void
        {
            if (!this.winChoose.isHide) 
            {
                return;
            }
            this.winChoose.show();
            GameProxy.copy.getRaceRooms();
            return;
        }

        /* internal  */enterRaceCopyHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SRaceRoom;
            if (loc1 == null) 
            {
                return;
            }
            GameProxy.copy.enterRace(loc1.roomId);
            return;
        }

        /* internal  */useSkillHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SRaceUseSkill;
            GameProxy.copy.useRaceSkill(loc1);
            return;
        }

        /* internal  */onSkillTransferOverRes(arg1: DataEvent): void
        {
            if (this.skillPanel.lastTarget != null) 
            {
                AIManager.onAIControl(this.skillPanel.lastTarget, AIType.AI_Scene);
            }
            return;
        }

        /* internal  */keyDownHandler(arg1: KeyEvent): void
        {
            if (!cache.copy.isInRaceCopy()) 
            {
                return;
            }
            var loc1=arg1.keyEvent;
            var loc2=loc1.keyCode;
            if (loc2 >= KeyCode.N1 && loc2 <= KeyCode.N4 || loc2 == KeyCode.Q || loc2 == KeyCode.W || loc2 == KeyCode.E || loc2 == KeyCode.R) 
            {
                this.skillPanel.useSkillByShortcutKey(loc1.keyCode);
            }
            return;
        }

        /* internal  */updateChooseWinTimer(): void
        {
            var loc1=0;
            if (this._winChoose != null) 
            {
                loc1 = (cache.copy.raceCopySchedule.startDt.time - ClockManager.instance.nowDate.time) / 1000;
                if (loc1 > 0) 
                {
                    this._winChoose.updateLeftTime(loc1);
                }
                else if (loc1 <= 0) 
                {
                    this._winChoose.setStarted();
                }
            }
            return;
        }

        private /* var */_icon: RaceCopyIconBtn;

        private /* var */_hintIcon: HintIcon;

        private /* var */_curResultData: SRaceResult;

        private /* var */_infoPanel: RaceCopyInfoPanel;

        private /* var */_winChoose: RaceCopyRoomChooseWin;

        private /* var */_skillPanel: RaceCopySkillPanel;

        private /* var */count: int=0;
    }
